package services

import (
	"errors"
	"fmt"
	robot "github.com/AnonymousMister/dingtalk-robot"
	"github.com/AnonymousMister/dingtalk-robot/template"
	"gitlab-dingtalk/server/model"
	"gitlab-dingtalk/server/repositories"
	"regexp"
	"strings"
)

var GitlabService = newGitlabService()

func newGitlabService() *gitlabService {
	return &gitlabService{}
}

type gitlabService struct {
}

func (s *gitlabService) Forward(gitlab *model.GitLab) error {
	s.format(gitlab)
	var err error
	switch gitlab.EventType {
	case "note":
		err = s.Note(gitlab)
	case "issue":
		err = s.Issue(gitlab)
	default:
		err = errors.New(fmt.Sprintf("%s is lesser than 50\n", gitlab.EventType))
	}
	return err
}

func (s *gitlabService) format(gitlab *model.GitLab) {
	if gitlab.IssuesText.Description != "" {
		tpl := strings.ReplaceAll(gitlab.IssuesText.Description, "![image]", "\n![image]")
		mkImage := regexp.MustCompile(`!\[image]\([\S]*\)`)
		match := mkImage.FindAllString(tpl, -1)
		for _, m := range match {
			tpl = strings.ReplaceAll(tpl, m, "")
		}
		gitlab.IssuesText.Description = strings.Trim(tpl, "\n")
	}
}

func (s *gitlabService) structureDingDing(gitlab *model.GitLab) *robot.DingDing {
	gitlab.Dingding.Type = "markdown"
	return gitlab.Dingding
}
func (s *gitlabService) getTemplate(gitlab *model.GitLab) (template.T, error) {
	var name string
	path := "./tpls"
	if gitlab.Changes == nil || gitlab.IssuesText.State != "opened" {
		return nil, errors.New("出现错误 不支持的 消息发送")
	}
	if gitlab.Changes.UpdatedAt != nil && gitlab.Changes.UpdatedAt.Previous == "" {
		name = "issue.md"
	} else if gitlab.Changes.Title != nil {
		name = "changes_title.md"
	} else if gitlab.Changes.Assignees != nil {
		name = "changes_assignees.md"
	} else if gitlab.Changes.Labels != nil {
		// 修改 Labels变动 不在发送消息
		//name = "labels.md"
	}
	if name != "" {
		return template.NewGoTemplate(name, path, gitlab), nil
	}
	return nil, errors.New("出现错误 不支持的 消息发送")
}
func (s *gitlabService) mobiles(gitlab *model.GitLab) (mobiles string, names string) {
	for i := 0; i < len(gitlab.Assignees); i++ {
		user, _ := repositories.SysUserRepositories.FindUserByName(gitlab.Assignees[i].Name)
		gitlab.Assignees[i].Mobile = user.Mobile
		if names == "" {
			names = gitlab.Assignees[i].Name
		} else {
			names = names + "," + gitlab.Assignees[i].Name
		}
		if user.Mobile != "" {
			mobiles = mobiles + "," + user.Mobile
		}
	}
	return mobiles, names
}
func (s *gitlabService) Issue(gitlab *model.GitLab) error {
	var mobiles, names = s.mobiles(gitlab)
	dingding := s.structureDingDing(gitlab)
	dingding.Title = fmt.Sprintf("[%s]你有一个issue回执要处理", names)
	dingding.Mobiles = mobiles
	var err error
	dingding.Template, err = s.getTemplate(gitlab)
	if err != nil {
		return err
	}
	return dingding.Exec()

}

func (s *gitlabService) Note(gitlab *model.GitLab) error {
	var mobiles, names = s.mobiles(gitlab)
	dingdingvar := s.structureDingDing(gitlab)
	dingdingvar.Title = fmt.Sprintf("[%s]你有一个issue回执要处理", names)
	dingdingvar.Mobiles = mobiles
	dingdingvar.Template = template.NewGoTemplate("note.md", "./tpls", gitlab)
	return dingdingvar.Exec()
}
